package com.web.servlet.validate;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONObject;

import com.web.model.User;
import com.web.service.ServiceDAOFactory;

@WebServlet("/validateEmail.do")
public class ValidateEmailServlet extends HttpServlet {
	
	private static final long serialVersionUID = 328747439745L;
	private boolean flag;
	
	@Override
	protected void doGet(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
		doPost(req,resp);
	}

	protected void doPost(HttpServletRequest req,HttpServletResponse resp) throws ServletException,IOException{
		String email = req.getParameter("email");
		
		try{
			User user= ServiceDAOFactory.getUserService().findByEmail(email);
			//System.out.println(user);
			if(user != null){
				//System.out.println("user存在");
				flag = false;
			}else{
				//System.out.println("user不存在");
				flag = true;
			}
			
			JSONObject json = new JSONObject();
			json.put("flag", flag);
			PrintWriter out = resp.getWriter();
			out.print(json.toString());
			out.close();
		}catch (Exception e){
			e.printStackTrace();
		}
	}
		
}
